#!/bin/sh
# generates self-extract archive with Plesk distribution

err()
{
	echo
	echo "ERROR!"
	echo "Check the error reason, fix it and try again"
	echo
	exit 1
}

unsp_ptf()
{
	ptf=$1

	echo
	echo "Sorry, but your configuration isn't supported yet"
	echo "uname is $ptf"
	echo
	echo "For more information, please contact support@plesk.com"
	echo
	exit 1
}

set_machine()
{
	TARGET_OS="`uname -a`"

	case "$TARGET_OS" in
		FreeBSD* | \
		Linux*)		compress="gzip -9 -c"
				uncompress="gunzip -c"
				;;
		BSD/OS*)	compress="compress"
				uncompress="uncompress"
				;;
		SunOS*5.*)	compress="compress -c"
				uncompress="uncompress -c"
				;;
		*)			unsp_ptf "$TARGET_OS"
					;;
	esac
}

usage()
{
	echo "usage: $0 <plesk_major.minor_version> <locale>"
	echo "example: $0 8.1 en-US"
}

set_machine


PRE_DIST_DIR=./dist

PLESK_VERSION=$1
LOCALE=$2

error="NO"

if [ -z "$PLESK_VERSION" ]; then
	error="YES"
else
	echo "$PLESK_VERSION" | grep '^[0-9]\.[0-9]$' > /dev/null
	if [ 0$? -ne 0 ]; then
		echo "Error: Wrong Plesk version is specified. You should specify Plesk major and minor versions: for example, 8.1 (for Plesk v.8.1.x)"
		error="YES"
	fi
fi

if [ -z "$LOCALE" ]; then
	error="YES"
else
	echo "$LOCALE" | grep '^[ \t]*[a-z][a-z]-[A-Z][A-Z][ \t]*$' >/dev/null
	if [ 0$? -ne 0 ]; then
		echo "Error: Locale isn't specified. You should specify a locale name: for example, en-US"
		error="YES"
	fi
fi

if [ "x$error" = "xYES" ]; then
	echo "Error: Wrong command format"
	usage
	exit 1
fi

if [ ! -d $PRE_DIST_DIR/htdocs/locales/$LOCALE ]; then
	echo "Error: $PRE_DIST_DIR/htdocs/locales/$LOCALE does not exist. Create this directory and copy your translated help files to it."
	exit 3
fi

if [ ! -d $PRE_DIST_DIR/plib/locales/$LOCALE ]; then
	echo "Error: $PRE_DIST_DIR/plib/locales/$LOCALE does not exist. Create this directory and copy your translated message and conhelp files to it."
	exit 4
fi


PLESK_VERSION=${PLESK_VERSION:=8.1}
LOCALE=${LOCALE:=en-US}
DATE=`date "+%y%m%d.%H"`
DISTNAME=${DISTNAME:=plesk_locale_${PLESK_VERSION}_build${DATE}_${LOCALE}_install.sh}
LOCALE_PACKET_DISTSIZE=`du -ks $PRE_DIST_DIR | awk '{print $1}'` 

curdir=`pwd`
#cd $PRE_DIST_DIR || err
sed \
 -e "s|@LOCALE_PACKET_DISTSIZE@|$LOCALE_PACKET_DISTSIZE|g" \
 -e "s|@PRODVERSION@|$PLESK_VERSION|g" \
 -e "s|@LOCALE@|$LOCALE|g" \
 <$curdir/head_install >$curdir/$DISTNAME || exit 1

chmod 644 $curdir/$DISTNAME
tar cf - -C $PRE_DIST_DIR . | gzip | uuencode psa_locale_$LOCALE.tar.gz >> $curdir/$DISTNAME || err
cd $curdir || err
chmod 755 $DISTNAME || err
echo "Locale $LOCALE done!"
exit 0

